<?php
declare (strict_types=1);

namespace app\admin\controller;

use think\response\Json;
use app\admin\model\User as UserModel;
use app\common\enum\Client as ClientEnum;
use think\facade\Db;

/**
 * 用户管理
 * Class User
 * @package app\store\controller
 */
class User extends Controller
{
    /**
     * 列表
     * @return Json
     */
    public function lists(): Json
    {
        $model = new UserModel;
        $list = $model->getList($this->request->param())->toArray();
        return $this->renderSuccess([
            'total' => $list['total'],
            'items' => $list['data']
        ]);
    }
    /**
     * 详情
     * @return Json
     */
    public function detail(int $id=0): Json
    {
        $model = new UserModel;
        $data = $model->detail($id)->toArray();
        return $this->renderSuccess($data);
    }
    /**
     * 修改状态
     * @return Json
     */
    public function setstatus(): Json
    {
        $params = $this->postData();
        $model = UserModel::detail($params['id']);
        // 更新记录
        if ($model->edit(['status' => $params['status']])) {
            return $this->renderSuccess('更新成功');
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }

    /**
     * 添加
     * @return Json
     */
    public function add(): Json
    {
        $model = new UserModel;
        if ($model->add($this->postForm())) {
            return $this->renderSuccess('添加成功');
        }
        return $this->renderError($model->getError() ?: '添加失败');
    }

    /**
     * 编辑
     * @param int $id
     * @return Json
     */
    public function edit(int $id): Json
    {
        $model = UserModel::detail($id);
        if ($model->edit($this->postForm())) {
            Db::name('guide')->where([
                ['user_id', '=', $model->user_id],
                ['role', '=', 'agent']
            ])->update([
                'name' => $model->nick_name,
                'tel' => $model->mobile
            ]);
            return $this->renderSuccess('更新成功');
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }

    /**
     * 删除
     * @param int $userId
     * @return Json
     */
    public function delete(int $id): Json
    {
        // 启动事务
        Db::startTrans();
        try {
            Db::name('user')->where('user_id', $id)->delete();
            // 删除用户收货地址
            Db::name('user_address')->where('user_id', $id)->delete();
            // 删除用户余额变动记录
            Db::name('user_balance_log')->where('user_id', $id)->delete();
            // 删除用户等级变动记录
            Db::name('user_grade_log')->where('user_id', $id)->delete();
            // 删除第三方用户信息
            Db::name('user_oauth')->where('user_id', $id)->delete();
            // 删除用户积分变动记录
            Db::name('user_points_log')->where('user_id', $id)->delete();
            // 删除用户充值订单套餐快照记录
            Db::name('recharge_order_plan')->where('order_id', 'IN', function ($query) use ($id) {
                $query->name('recharge_order')->where('user_id', $id)->field('order_id');
            })->delete();
            // 删除用户充值订单记录
            Db::name('recharge_order')->where('user_id', $id)->delete();
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return $this->renderError('删除失败');
        }
        return $this->renderSuccess('删除成功');
    }

    /**
     * 用户充值
     * @param int $userId
     * @param string $target
     * @return Json
     */
    public function recharge(int $userId, string $target): Json
    {
        $model = UserModel::detail($userId);
        if ($model->recharge($target, $this->postForm(), $this->admin['user_name'])) {
            return $this->renderSuccess('操作成功');
        }
        return $this->renderError($model->getError() ?: '操作失败');
    }

    /**
     * 修改会员等级
     * @param int $id
     * @return Json
     */
    public function setgrade(int $id): Json
    {
        $model = UserModel::detail($id);
        if ($model->updateGrade($this->postForm())) {
            return $this->renderSuccess('操作成功');
        }
        return $this->renderError($model->getError() ?: '操作失败');
    }

    /**
     * 客户端类型列表
     * @return Json
     */
    public function platform(): Json
    {
        $list = ClientEnum::data();
        $res = [];
        foreach ($list as $key => $value) {
            $res[] = $value;
        }
        return $this->renderSuccess($res);
    }
}
